package users;

import utils.Utils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class LoginFun {
    //展示弹框
    public static void showJDialog(String content){
        //创建一个弹框对象
        JDialog jDialog=new JDialog();
        //给弹框设置大小
        jDialog.setSize(300,200);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭，无法操作下面的界面
        jDialog.setModal(true);
        /*//取消居中放置
        jDialog.getContentPane().setLayout(null);*/
        // 取消布局管理器以手动设置组件位置
        jDialog.getContentPane().setLayout(null);

        // 创建管理文本的容器（修改为文本居中对齐）
        JLabel warning = new JLabel(content);
        warning.setHorizontalAlignment(SwingConstants.CENTER); // 水平居中
        warning.setVerticalAlignment(SwingConstants.CENTER); // 垂直居中
        warning.setBounds(0, 0, 300, 170); // 注意高度可以调整以适应文本

        /*//创建管理图片的容器
        JLabel warning=new JLabel(content);
        warning.setBounds(0,0,300,170);*/

        //将容器放到弹框中
        jDialog.getContentPane().add(warning);
        //弹框显示
        jDialog.setVisible(true);
    }

    //查询username为username1的用户/
    public static boolean userSearch(String username1)throws Exception{
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="select * from user where username=?" ;

        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1,username1);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            String rightUsername=rs.getString("username");
            if(username1.equals(rightUsername)){
                return true;
            }
        }
        Utils.Close(stmt,connection);
        return false;
    }

    //确认数据库中是否有用户名为username1,密码为password1的用户
    public static boolean userContain(String username1,String password1,String power)throws Exception{
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql;
        if(power.equals("0")){
            sql="select * from user where username=? and password=? and token='0'" ;
        }else if(power.equals("1")){
            sql="select * from user where username=? and password=? and token='1'" ;
        }else if(power.equals("-1")){
            sql="select * from user where username=? and password=? and token='-1'" ;
        }else {
            // 处理power值既不是"0"也不是"1"和"-1"的情况
            throw new IllegalArgumentException("Invalid power value: " + power);
        }
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1,username1);
        stmt.setString(2,password1);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            String rightUsername=rs.getString("username");
            String rightPassword=rs.getString("password");
            if(username1.equals(rightUsername)&&password1.equals(rightPassword)){
                return true;
            }
        }
        Utils.Close(stmt,connection);
        return false;
    }

    //添加用户到数据库中
    public static void addUser(String username,String password,String name,String gender,String telephone,String address,String power) throws SQLException {
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="insert into user(username,password,name,gender,telephone,address,token) values(?,?,?,?,?,?,?)";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1,username);
        stmt.setString(2,password);
        stmt.setString(3,name);
        stmt.setString(4,gender);
        stmt.setString(5,telephone);
        stmt.setString(6,address);
        stmt.setString(7,power);
        stmt.executeUpdate();
        if(power=="0"){
            showJDialog("已注册成普通用户!");
        }else if(power=="-1"){
            sql="insert into application(applicant_name,processor_name,result) values(?,'暂无','0')";
            stmt = connection.prepareStatement(sql);
            stmt.setString(1,username);
            stmt.executeUpdate();
            showJDialog("已提交管理员注册申请，请等待审核!");
        }
        Utils.Close(stmt,connection);
    }



}
